<template>
  <view class="uni-container">
   <uni-forms ref="form" :model="formData" validateTrigger="bind" label-width="200">
     <uni-forms-item name="title" label="标题">
       <uni-easyinput placeholder="请输入应用标题，最多8个汉字" v-model="formData.title"></uni-easyinput>
     </uni-forms-item>
     <uni-forms-item name="ftitle" label="副标题">
       <uni-easyinput placeholder="请输入应用副标题，显示在应用详情内" v-model="formData.ftitle"></uni-easyinput>
     </uni-forms-item>
	 <uni-forms-item name="model" label="接口模型">
	   <uni-easyinput placeholder="请输入接口模型名称,如:qianwen-max" v-model="formData.model"></uni-easyinput>
	 </uni-forms-item>
     <uni-forms-item name="keyword" label="关键词">
       <uni-easyinput placeholder="请输入应用关键词，此关键词为提交给接口的关键，决定生成内容的结果" v-model="formData.keyword"></uni-easyinput>
     </uni-forms-item>
     <uni-forms-item name="image" label="图片">
       <uni-file-picker @success="success" v-model="image" limit="1"></uni-file-picker>
     </uni-forms-item>
     <uni-forms-item name="describe" label="应用说明">
       <uni-easyinput placeholder="请输入应用说明" v-model="formData.describe"></uni-easyinput>
     </uni-forms-item>
     <uni-forms-item name="textarea_font_number" label="文本框字数">
       <uni-easyinput type="number" v-model="formData.textarea_font_number"></uni-easyinput>
     </uni-forms-item>
     <uni-forms-item name="textarea_placeholder" label="文本框说明">
       <uni-easyinput v-model="formData.textarea_placeholder" placeholder="文本框内的提示文本" type="textarea"></uni-easyinput>
     </uni-forms-item>
	 <uni-forms-item name="content" label="预设文本">
	   <uni-easyinput v-model="formData.content" placeholder="请输入预设文本,如:人物,时间,地点" type="textarea"></uni-easyinput>
	 </uni-forms-item>
     <uni-forms-item name="app_type" label="应用类型">
       <uni-section title="本地数据" type="line">
             <uni-data-select
               v-model="formData.app_type"
               :localdata="appType"
               @change="change"
             ></uni-data-select>
           </uni-section>
     </uni-forms-item>
     <uni-forms-item name="type" label="应用分类">
       <uni-section title="本地数据" type="line">
             <uni-data-select
               v-model="formData.type"
               collection="application_class"
               field="_id as value, title as text"
               @change="change"
             ></uni-data-select>
           </uni-section>
     </uni-forms-item>
     <uni-forms-item name="is_show_copy_link" label="显示粘贴连接">
       <switch @change="binddata('is_show_copy_link', $event.detail.value)" :checked="formData.is_show_copy_link"></switch>
     </uni-forms-item>
     <view class="uni-button-group">
       <button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
       <navigator open-type="navigateBack" style="margin-left: 15px;">
         <button class="uni-button" style="width: 100px;">返回</button>
       </navigator>
     </view>
   </uni-forms>
  </view>
</template>

<script>
  import { validator } from '../../js_sdk/validator/applications.js';

  const db = uniCloud.database();
  const dbCmd = db.command;
  const dbCollectionName = 'applications';

  function getValidator(fields) {
    let result = {}
    for (let key in validator) {
      if (fields.includes(key)) {
        result[key] = validator[key]
      }
    }
    return result
  }

  

  export default {
    data() {
      let formData = {
        "title": "",
        "ftitle": "",
        "keyword": "",
        "image": "",
		"content":"",
		"model":"",
        "describe": "",
        "textarea_font_number": 500,
        "textarea_placeholder": "",
        "case": "",
        "app_type": null,
        "type": "",
        "use_number": 0,
        "sort": 0,
        "is_show_textarea": true,
        "is_show_copy_link": false,
        "create_uid": "",
        "create_date": null
      }
      return {
		appType:[
			{
				value:1,
				text:'生成文案'
			},
			{
				value:2,
				text:'生成视频'
			},
			{
				value:3,
				text:'口播视频'
			},
			{
				value:4,
				text:'只配音'
			}
		],
		image:[],
        formData,
        formOptions: {},
        rules: {
          ...getValidator(Object.keys(formData))
        }
      }
    },
    onLoad(e) {
      if (e.id) {
        const id = e.id
        this.formDataId = id
        this.getDetail(id)
      }
    },
    onReady() {
      this.$refs.form.setRules(this.rules)
    },
    methods: {
      //上传成功以后
      success(e){
		this.formData.image = e.tempFilePaths[0];
      },
      /**
       * 验证表单并提交
       */
      submit() {
        uni.showLoading({
          mask: true
        })
        this.$refs.form.validate().then((res) => {
          return this.submitForm(res)
        }).catch(() => {
        }).finally(() => {
          uni.hideLoading()
        })
      },

      /**
       * 提交表单
       */
      submitForm(value) {
        // 使用 clientDB 提交数据
        return db.collection(dbCollectionName).doc(this.formDataId).update(value).then((res) => {
          uni.showToast({
            title: '修改成功'
          })
          this.getOpenerEventChannel().emit('refreshData')
          setTimeout(() => uni.navigateBack(), 500)
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        })
      },

      /**
       * 获取表单数据
       * @param {Object} id
       */
      getDetail(id) {
        uni.showLoading({
          mask: true
        })
        db.collection(dbCollectionName).doc(id).field("title,ftitle,keyword,image,describe,textarea_font_number,textarea_placeholder,case,app_type,type,use_number,sort,is_show_textarea,is_show_copy_link,create_uid,create_date").get().then((res) => {
          const data = res.result.data[0]
          if (data) {
            this.formData = data
			this.image = {
				path:data.image,
				fileID:data.image,
				url:data.image
			}
            
          }
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        }).finally(() => {
          uni.hideLoading()
        })
      }
    }
  }
</script>
